#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/syscall.h>
#include <time.h>
#include <string.h>

#define MSG "OMG, I catch the signal SIGINT\n"
#define MSG_END "OK,I finished the signal SIGINT\n"

int do_heavy_work()
{
    int i, k;
    srand(time(NULL));
    for (i = 0; i < 100000000; i++)
    {
        k = rand() % 123456789;
    }
    return 0;
}
void signal_handler(int signo)
{
    write(2, MSG, strlen(MSG));
    do_heavy_work();
    write(2, MSG_END, strlen(MSG_END));
}
int main()
{
    char input[1024] = {0};
#if defined SYSCALL_SIGNAL_API
    if (syscall(SYS_SIGNAL, SIGINT, signal_handler) == -1)
    {
    }
#elif defined SYSV_SIGNAL_API
    if (sys_signal(SIGINT, signal_handler) == SIG_ERR)
#elif defined BSD_SIGNAL_API
    if (bsd_signal(SIGINT, signal_handler) == SIG_ERR)
#else
    if (signal(SIGINT, signal_handler) == SIG_ERR)
#endif
    {
        printf("signal failed.\n");
        exit(-2);
    }
    printf("input string:\n");
    if (fgets(input, sizeof(input), stdin) == NULL)
    {
        printf("fgets failed.\n");
        exit(-3);
    }
    printf("You input is: %s", input);
    return 0;
}